// pages/common/company-detail/index.js
import {getRecruitmentList} from "../../../service/Position";
import {getRegisterDetail} from "../../../service/Enterprise";
import {geocoder} from "../../../utils/Location";
import {openLocation} from "../../../utils/Wx";
import Config from "../../../main.config";
import Toast from "@vant/weapp/toast/toast";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageShow: false,
        errShow: false,
        detail: {},
        //招聘信息
        recruitment: {
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: []
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data._companyId = options.companyId;
        this.getCompanyDetail();
    },

    /*
    * 获取企业详情
    * */
    getCompanyDetail: async function () {
        try {
            let companyId = this.data._companyId;
            let res = await getRegisterDetail({id: companyId});
            let logoUrl = Config.api.downLoad + "?fileId=" + res.logo
            this.data._enterpriseDetail = res;
            this.setData({
                detail: {
                    logo: logoUrl,
                    name: res.name,
                    nature: res.nature,
                    scale: res.scale,
                    industryName: res.industryName,
                    introduction: decodeURIComponent(res.introduction || ""),
                    addressDetail: res.addressDetail
                },
                pageShow: true,
                "recruitment.loading": true
            })
        } catch (e) {
            Toast.fail("获取失败！");
            this.setData({errShow: true});
            console.error(e)
        }
    },

    /*
    * 列表加载
    * */
    onLoadList: async function (e) {
        try {
            let companyId = this.data._companyId;
            let paging = e.detail.paging;
            let list = this.data.recruitment.list;
            let isReset = this.data.recruitment.reset;
            let res = await getRecruitmentList({
                type: 1, //在招职位
                companyId: companyId,
                page: paging.page,
                limit: paging.pageSize,
            });
            let newList = res.data;
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "recruitment.list": list,
                "recruitment.total": res.count
            });
        } catch (e) {
            this.setData({
                "recruitment.error": true
            })
        } finally {
            this.setData({
                "recruitment.reset": false,
                "recruitment.loading": false
            })
        }
    },

    /*
    * 打开地图
    * */
    bindOpenLocation: async function () {
        try {
            let addressDetail = this.data.detail.addressDetail;
            let location = await geocoder(addressDetail);
            await openLocation(location)
        } catch (e) {
            Toast.fail("打开地图失败！");
            console.error(e)
        }
    },

    /*
    * 点击跳转详情
    * */
    bindNavigatorToRecruitmentDetail: function (e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/pages/common/recruitment-detail/index?id=" + id
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({"recruitment.loading": true})
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})